package com.beauty.logistics.controller;

import com.beauty.logistics.dto.CarrierRequest;
import com.beauty.logistics.dto.CarrierResponse;
import com.beauty.logistics.result.Result;
import com.beauty.logistics.service.CarrierService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/carrier")
@RequiredArgsConstructor
@Slf4j
public class CarrierController {
    
    private final CarrierService carrierService;
    
    @PostMapping
    public Result<CarrierResponse> createCarrier(@Valid @RequestBody CarrierRequest carrierRequest) {
        try {
            log.info("【物流服务】接收到创建承运商请求: {}", carrierRequest.getName());
            return Result.success(carrierService.createCarrier(carrierRequest));
        } catch (Exception e) {
            log.error("【物流服务】创建承运商失败", e);
            return Result.error("创建承运商失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/{id}")
    public Result<CarrierResponse> getCarrierById(@PathVariable Long id) {
        try {
            log.info("【物流服务】接收到查询承运商请求, ID: {}", id);
            return Result.success(carrierService.getCarrierById(id));
        } catch (Exception e) {
            log.error("【物流服务】查询承运商失败", e);
            return Result.error("查询承运商失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/name/{name}")
    public Result<CarrierResponse> getCarrierByName(@PathVariable String name) {
        try {
            log.info("【物流服务】接收到按名称查询承运商请求: {}", name);
            return Result.success(carrierService.getCarrierByName(name));
        } catch (Exception e) {
            log.error("【物流服务】按名称查询承运商失败", e);
            return Result.error("按名称查询承运商失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/code/{code}")
    public Result<CarrierResponse> getCarrierByCode(@PathVariable String code) {
        try {
            log.info("【物流服务】接收到按代码查询承运商请求: {}", code);
            return Result.success(carrierService.getCarrierByCode(code));
        } catch (Exception e) {
            log.error("【物流服务】按代码查询承运商失败", e);
            return Result.error("按代码查询承运商失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/active")
    public Result<List<CarrierResponse>> getActiveCarriers() {
        try {
            log.info("【物流服务】接收到查询活跃承运商请求");
            return Result.success(carrierService.getActiveCarriers());
        } catch (Exception e) {
            log.error("【物流服务】查询活跃承运商失败", e);
            return Result.error("查询活跃承运商失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/country/{country}")
    public Result<List<CarrierResponse>> getCarriersByCountry(@PathVariable String country) {
        try {
            log.info("【物流服务】接收到按国家查询承运商请求: {}", country);
            return Result.success(carrierService.getCarriersByCountry(country));
        } catch (Exception e) {
            log.error("【物流服务】按国家查询承运商失败", e);
            return Result.error("按国家查询承运商失败: " + e.getMessage());
        }
    }
    
    @GetMapping
    public Result<List<CarrierResponse>> getAllCarriers() {
        try {
            log.info("【物流服务】接收到查询所有承运商请求");
            List<CarrierResponse> carriers = carrierService.getAllCarriers();
            log.info("【物流服务】成功查询到{}个承运商", carriers.size());
            return Result.success(carriers);
        } catch (Exception e) {
            log.error("【物流服务】查询所有承运商失败", e);
            return Result.error("查询所有承运商失败: " + e.getMessage());
        }
    }
    
    @PutMapping("/{id}")
    public Result<CarrierResponse> updateCarrier(
            @PathVariable Long id,
            @Valid @RequestBody CarrierRequest carrierRequest) {
        try {
            log.info("【物流服务】接收到更新承运商请求, ID: {}", id);
            return Result.success(carrierService.updateCarrier(id, carrierRequest));
        } catch (Exception e) {
            log.error("【物流服务】更新承运商失败", e);
            return Result.error("更新承运商失败: " + e.getMessage());
        }
    }
    
    @PutMapping("/{id}/status")
    public Result<CarrierResponse> toggleCarrierStatus(
            @PathVariable Long id,
            @RequestParam Boolean isActive) {
        try {
            log.info("【物流服务】接收到更新承运商状态请求, ID: {}, 状态: {}", id, isActive);
            return Result.success(carrierService.toggleCarrierStatus(id, isActive));
        } catch (Exception e) {
            log.error("【物流服务】更新承运商状态失败", e);
            return Result.error("更新承运商状态失败: " + e.getMessage());
        }
    }
    
    @DeleteMapping("/{id}")
    public Result<Void> deleteCarrier(@PathVariable Long id) {
        try {
            log.info("【物流服务】接收到删除承运商请求, ID: {}", id);
            carrierService.deleteCarrier(id);
            return Result.success();
        } catch (Exception e) {
            log.error("【物流服务】删除承运商失败", e);
            return Result.error("删除承运商失败: " + e.getMessage());
        }
    }
} 